package icu.qx.wm.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import icu.qx.wm.domain.WmStorageLocation;
import icu.qx.wm.service.IWmStorageLocationService;
import icu.qx.wm.vo.WarehouseFormVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/wm/warehouse/location")
public class WmStorageLocationController extends BaseController {
    private final IWmStorageLocationService wmStorageLocationService;

    @Autowired
    public WmStorageLocationController(IWmStorageLocationService IWmStorageLocationService) {
        this.wmStorageLocationService = IWmStorageLocationService;
    }

    /**
     * 查询库位列表
     */
    @RequiresPermissions("wm:warehouse:location:list")
    @GetMapping("/list")
    public TableDataInfo list(WmStorageLocation wmStorageLocation) {
        startPage();
        List<WmStorageLocation> list = wmStorageLocationService.selectWmStorageLocationList(wmStorageLocation);
        return getDataTable(list);
    }

    /**
     * 导出库位列表
     */
    @RequiresPermissions("wm:warehouse:location:export")
    @Log(title = "库位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmStorageLocation wmStorageLocation) {
        List<WmStorageLocation> list = wmStorageLocationService.selectWmStorageLocationList(wmStorageLocation);
        ExcelUtil<WmStorageLocation> util = new ExcelUtil<>(WmStorageLocation.class);
        util.exportExcel(response, list, "库位数据");
    }

    /**
     * 获取库位详细信息
     */
    @RequiresPermissions("wm:warehouse:location:query")
    @GetMapping(value = "/{slCode}")
    public AjaxResult getInfo(@PathVariable("slCode") String slCode) {
        return success(wmStorageLocationService.selectWmStorageLocationBySlCode(slCode));
    }

    /**
     * 新增库位
     */
    @RequiresPermissions("wm:warehouse:location:add")
    @Log(title = "库位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmStorageLocation wmStorageLocation) {
        return toAjax(wmStorageLocationService.insertWmStorageLocation(wmStorageLocation));
    }

    /**
     * 修改库位
     */
    @RequiresPermissions("wm:warehouse:location:edit")
    @Log(title = "库位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmStorageLocation wmStorageLocation) {
        return toAjax(wmStorageLocationService.updateWmStorageLocation(wmStorageLocation));
    }

    /**
     * 删除库位
     */
    @RequiresPermissions("wm:warehouse:location:remove")
    @Log(title = "库位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{slCodes}")
    public AjaxResult remove(@PathVariable String[] slCodes) {
        return toAjax(wmStorageLocationService.deleteWmStorageLocationBySlCodes(slCodes));
    }

    /**
     * 查询库位编码
     */
    @RequiresPermissions("wm:warehouse:location:query")
    @GetMapping("/list/codes")
    public AjaxResult getSlCode(WarehouseFormVo warehouseFormVo) {
        return success(wmStorageLocationService.selectLocationCodes(warehouseFormVo));
    }
}
